package com.cw.jeeyt.controller.facade;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;

import com.cw.jeeyt.service.bo.facade.FacadeEnvBo;
import com.cw.jeeyt.service.facade.FacadeEnvService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;
/**
 * 环境信息控制层
 *
 * @author chenw
 * @date 2019-10-31 23:19:05
 */
@Controller
@RequestMapping( "/facadeEnv" )
public class FacadeEnvController extends BaseController{
	@Resource
	private FacadeEnvService facadeEnvService;
	/**
	 * 跳转到环境信息列表查询页面
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value = "/query" )
	public ModelAndView query() throws Exception {

		ModelAndView mv = new ModelAndView("facade/facadeEnv_index");
		mv.addObject("permission", new PermissionDirective());
		mv.addObject("PageDict", new PageDictDirective());
		return mv;
	}
	/**
	 * 加载DataTable列表数据
	 *
	 * @param dataTable datatable数据
	 * @return
     */
	@ResponseBody
	@RequestMapping( value = "/listFacadeEnv")
	public ResultData listFacadeEnv(@RequestBody DataTable dataTable) {

		FacadeEnvBo facadeEnvBo = JSONObject.parseObject(JSON.toJSONString(dataTable.queryCond),FacadeEnvBo.class);
		if(facadeEnvBo == null){
			facadeEnvBo = FacadeEnvBo.builder().build();
		}

		PageParam pageParam = new PageParam();
        pageParam.setPageNum(PageUtil.calPageNum(dataTable.getStart(),dataTable.getLength()));
        pageParam.setPageSize(dataTable.getLength());
		PageInfo<FacadeEnvBo> pageInfo = facadeEnvService.selectPage(facadeEnvBo,pageParam);
		return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
	}

	/**
	 * 跳转到环境信息表单页面
	 *
	 * @param operation 操作  add 新增 edit修改 view查看
	 * @return
     */
	@RequestMapping( value = "/to-form" )
	public ModelAndView editEdit(Long id,String operation) {

		ModelAndView mv = new ModelAndView("facade/facadeEnv_form");
		FacadeEnvBo facadeEnvBo = null;
		if(EnumOperateFlag.ADD.getCode().equals(operation)){
			facadeEnvBo = FacadeEnvBo.builder().build();
		}else{
			facadeEnvBo = facadeEnvService.selectByKey(id);
		}
		mv.addObject("facadeEnv", facadeEnvBo);
		mv.addObject("operation", operation);
		mv.addObject("PageDict", new PageDictDirective());
		mv.addObject("permission", new PermissionDirective());
		return mv;
	}
	/**
	 * 根据操作类型保存环境信息数据
	 *
	 * @param facadeEnvBo
	 * @param operation add 新增  edit 修改
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/saveFacadeEnv",method=RequestMethod.POST)
	public JsonResultDto saveFacadeEnv(FacadeEnvBo facadeEnvBo,String operation) throws Exception{

		JsonResultDto jsonResultDto = new JsonResultDto(false);
		try{
			if(operation.equals(EnumOperateFlag.ADD.getCode())){
				facadeEnvService.addFacadeEnv(facadeEnvBo);
			}else if(operation.equals(EnumOperateFlag.EDIT.getCode())){
				facadeEnvService.modifyFacadeEnv(facadeEnvBo);
			}
			jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			jsonResultDto.setMsg("保存失败:" + e.getMessage());
		}
		return jsonResultDto;
	}
	/**
	 * 删除环境信息
	 *
	 * @param facadeEnvBo
	 * @throws Exception
	 */
	@RequestMapping( value = "/delete" )
	@ResponseBody
	public JsonResultDto delete(FacadeEnvBo facadeEnvBo) throws Exception{

		JsonResultDto jsonResultDto = new JsonResultDto(false);
		try{
			facadeEnvService.deleteFacadeEnv(facadeEnvBo);
			jsonResultDto.setMsg("删除成功");
            jsonResultDto.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			jsonResultDto.setMsg("删除失败:" + e.getMessage());
		}
		return jsonResultDto;
	}
}
